<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<title>表单</title>
	<link rel="stylesheet" href="${ctx}/static/admin/layui/css/layui.css">
	<link rel="stylesheet" href="${ctx}/static/admin/css/formSelects-v4.css">
</head>
<script>

</script>
<body>
<div class="layui-card-body">
	<form class="layui-form layui-form-pane">
		<input name="modules.id" type="hidden" value="${item.id! }" />
        <input name="competenceid" id="competenceid" type="hidden" value="" />
		<div class="layui-row">
			<div class="layui-col-xs6">
				<div class="layui-form-item">
					<label class="layui-form-label">姓名<font style="color: red">*</font></label>
					<div class="layui-input-block">
						<select name="userid" id="userid" xm-select="userid" lay-verify="required"  xm-select-radio xm-select-search  ></select>
					</div>
				</div>
			</div>
			<div class="layui-col-xs6">
				<div class="layui-form-item" pane="">
					<label class="layui-form-label">数据权限<font style="color: red">*</font></label>
					<div class="layui-input-block">
						<input type="radio" name="type"  value="1" id="radio1" lay-verify="required" title="本部门" checked >
						<input type="radio" name="type"  value="2" id="radio2" lay-verify="required" title="全公司" >
					</div>
				</div>
			</div>
		</div>
		<div class="layui-form-item" style="display: none">
			<div class="layui-input-block">
				<button id="submit-btn" class="layui-btn" lay-submit >立即提交</button>
				<button id="reset-btn" type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>
    <#button permission="competencebtnadd" iconCls="layui-btn" size="layui-btn-sm" textName="添加" id="add"/>
    <#button permission="competencebtnupd" iconCls="layui-btn" size="layui-btn-sm" textName="修改" id="update"/>
    <table id="usertable" lay-filter="user"></table>
</div>
</body>
</html>
<script type="text/html" id="toolbar">
    <#button permission="competencebtnedit" iconCls="layui-btn" size="layui-btn-xs" textName="编辑" event="edit"/>
    <#button permission="competencebtndel" iconCls="layui-btn-danger" size="layui-btn-xs" textName="删除" event="del"/>
</script>
<script src="${ctx}/static/admin/layui/layui.js"></script>
<script src="${ctx}/static/admin/js/common.js"></script>
<script>
    var table, params, $, ai = 0, newtype=1;
    layui.use(['jquery','form','table','formSelects'], function(){
        $ = jQuery = layui.$;
        var form = layui.form;
        var formSelects = layui.formSelects;
        var table = layui.table;
        tableIns = table.render({
            elem: '#usertable',
            height:'full-123',
            size:'sm',
            url: '/sys_admin/modules/userCompetence?moduleid=${item.id! }',
            page: true,
            even: true,
            cols: [[
                {type: 'numbers',fixed: 'left'},
                {field: 'id', title: 'ID',fixed: 'left', align:'center',width:180, hide:true},
                {field: 'username', title: '姓名',fixed: 'left', align:'center',width:200},
                {field: 'type', title: '权限', align:'center', width:150,
                    templet: function(d){
                        if (d.type == 1) {
                            return '本部门';
                        } else if (d.type == 2) {
                            return '全公司';
                        }
                    }
                },
                {title: '操作', width: 400, fixed: 'right', align:'center',toolbar: '#toolbar'}
            ]]
        });
        formSelects.data('userid', 'server', {
            url: '${ctx}/sys_admin/sys/user/getData?moduleid=${item.id! }',
            keyword: 'username',
            keyVal: 'id',
            /*
            success: function(id, url, searchVal, result){
                formSelects.value('userid', ['${pid}']);
            }*/
        });
        $("#add").on('click', function() {
            if(newtype==2){
                formSelects.data('userid', 'server', {
                    url: '${ctx}/sys_admin/sys/user/getData?moduleid=${item.id! }',
                    keyword: 'username',
                    keyVal: 'id',
                });
                layer.msg("请先选择用户!");
                newtype=1;
            }
            var user = formSelects.value('userid');
            if(user.length<=0){
                layer.msg("请先选择用户!");
                return;
            }
            var userid = user[0].id;
            var competenceid = $("#competenceid").val();
            var type = $('input[name="type"]:checked').val();
            $.post('${ctx}/sys_admin/modules/saveCompetence',{"userid":userid,"type":type,"moduleid":"${item.id! }"}, function(result) {
                if (result.success) {
                    layer.msg("添加成功");
                    formSelects.data('userid', 'server', {
                        url: '${ctx}/sys_admin/sys/user/getData?moduleid=${item.id! }',
                        keyword: 'username',
                        keyVal: 'id',
                    });
                    tableIns.reload({});
                } else {
                    layer.msg(result.msg);
                }
            }, 'json');
        });
        table.on('tool(user)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确定删除行么？', function(index){
                    $.post('/sys_admin/modules/deleteCompetence', {"id":data.id}, function(result) {
                        if (result) {
                            layer.msg("删除成功!");
                            formSelects.data('userid', 'server', {
                                url: '${ctx}/sys_admin/sys/user/getData?moduleid=${item.id! }',
                                keyword: 'username',
                                keyVal: 'id',
                            });
                            tableIns.reload({});
                        } else {
                            layer.msg(result.msg);
                        }
                    }, 'json');
                });
            } else if(obj.event === 'edit'){
                newtype=2;
                $("#competenceid").val(data.id);
                formSelects.data('userid', 'server', {
                    url: '${ctx}/sys_admin/sys/user/getDataOne?userid='+data.user,
                    keyword: 'username',
                    keyVal: 'id',
                    success: function(id, url, searchVal, result){
                        formSelects.value('userid', [data.user]);
                    }
                });
                if(data.type==1){
                    $("#radio1").prop("checked","checked");
                    $("#radio2").removeAttr("checked");
                    form.render();
                }else if(data.type==2){
                    $("#radio2").prop("checked","checked");
                    $("#radio1").removeAttr("checked");
                    form.render();
                }
            }
        });
        $("#update").on('click', function() {
            if(newtype==1){
                return ;
            }
            var user = formSelects.value('userid');
            if(user.length<=0){
                layer.msg("请先选择用户!");
                return;
            }
            var userid = user[0].id;
            var competenceid = $("#competenceid").val();
            var type = $('input[name="type"]:checked').val();
            $.post('${ctx}/sys_admin/modules/updateCompetence',{"competenceid":competenceid,"userid":userid,"type":type,"moduleid":"${item.id! }"}, function(result) {
                if (result.success) {
                    layer.msg("修改成功");
                    formSelects.data('userid', 'server', {
                        url: '${ctx}/sys_admin/sys/user/getData?moduleid=${item.id! }',
                        keyword: 'username',
                        keyVal: 'id',
                    });
                    tableIns.reload({});
                } else {
                    layer.msg(result.msg);
                }
            }, 'json');
        });
    });
</script>
